﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFTouch.Examples
{
    /// <summary>
    /// Example2.xaml 的交互逻辑
    /// </summary>
    public partial class Example2 : Window
    {
        public Example2()
        {
            InitializeComponent();
        }

        /// <summary>
        ///  当触碰到Image 图片时，image_ManipulationStarting 事件将会自动触发，
        ///  首先需要定义ManipulationContainer（即为touchPad<Canvas>），该容器的主要用于定义参考坐标，图片的任何操作均以参考坐标为准。
        ///  ManipulationModes 设置可以限制哪些手势操作是程序允许的，如果没有特殊情况可设置为"All"。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void image_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            e.ManipulationContainer = touchPad;
            e.Mode = ManipulationModes.All;
        }

        /// <summary>
        /// ManipulationDelta 事件会在手势操作开始时触发，并且该手势需持续进行不得间断。
        /// 通过FrameworkElement 获得接受操作的图片控件，将图片透明度降低到0.5，创建Matrix 用于控制图片MatrixTransform，
        /// 利用Point 获得图片中心坐标点，通过ScaleAt、RotateAt、Translate 执行缩放、旋转、平移操作。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            FrameworkElement element = e.Source as FrameworkElement;
            if (element != null)
            {

                
                //element.Opacity = 0.5;
                //Matrix matrix = ((MatrixTransform)element.RenderTransform).Matrix;
                //var deltaManipulation = e.DeltaManipulation;
                //Point center = new Point(element.ActualWidth / 2, element.ActualHeight / 2);
                //center = matrix.Transform(center);
                //matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y);
                //matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
                //matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
                //((MatrixTransform)element.RenderTransform).Matrix = matrix;
                try
                {
                    //在设置了惯性事件后，如果不处理判断控件容器的边界，那很容易一个移动就会把控件移到屏幕外部
                    if (e.IsInertial)
                    {
                        Rect containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);

                        Rect shapeBounds = element.RenderTransform.TransformBounds(new Rect(element.RenderSize));
                        if (e.IsInertial && !containingRect.Contains(shapeBounds))
                        {
                            e.ReportBoundaryFeedback(e.DeltaManipulation);
                            e.Complete();
                        }
                    }

                    ManipulationDelta deltaManipulation = e.DeltaManipulation;
                    Matrix matrix = element.RenderTransform.Value;
                    Point center = new Point(element.ActualWidth / 2, element.ActualHeight / 2);
                    center = matrix.Transform(center);  //设置中心点
                    //处理缩放
                    matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y);
                    // 处理旋转
                    matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
                    //处理移动

                    matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

                    element.RenderTransform = new MatrixTransform(matrix);

                    e.Handled = true;
                }
                catch (Exception ei)
                {
                    MessageBox.Show(ei.ToString());
                }
            }
           
        }

        /// <summary>
        /// 当手指离开触摸屏即操作结束，image_ManipulationCompleted 事件触发，将图片透明度重新调整为1。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void image_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)e.Source;
            element.Opacity = 1;
        }

        private void image_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
        {
            // 移动惯性
            e.TranslationBehavior = new InertiaTranslationBehavior();
            e.TranslationBehavior.InitialVelocity = e.InitialVelocities.LinearVelocity;
            e.TranslationBehavior.DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0);// 单位：一个WPF单位 / ms

            // 缩放惯性
            e.ExpansionBehavior = new InertiaExpansionBehavior();
            e.ExpansionBehavior.InitialVelocity = e.InitialVelocities.ExpansionVelocity;
            e.ExpansionBehavior.DesiredDeceleration = 0.1 * 96 / 1000.0 * 1000.0;// 单位：一个WPF单位 / ms

            // 旋转惯性
            e.RotationBehavior = new InertiaRotationBehavior();
            e.RotationBehavior.InitialVelocity = e.InitialVelocities.AngularVelocity;
            e.RotationBehavior.DesiredDeceleration = 720 / (1000.0 * 1000.0);//单位：一个角度 / ms
            e.Handled = true;
        }
    }
}
